//
//  MineShenController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/9/27.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "MineShenController.h"
#import "MineWorkCell.h"
#import "WorkModel.h"
@interface MineShenController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * wTableVeiw;
@property(nonatomic,strong)NSMutableArray * workArray;
@property(nonatomic,strong)WorkModel * workModel;
@end

@implementation MineShenController
{
    int _pageNo;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _pageNo = 0;
    self.title = @"我的申请";
    _workArray = [NSMutableArray array];
    [self createwTableView];
    [self requestDai];
}
-(void)createwTableView
{
    _wTableVeiw=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStylePlain];
    _wTableVeiw.delegate=self;
    _wTableVeiw.dataSource=self;
    _wTableVeiw.showsVerticalScrollIndicator=NO;
    _wTableVeiw.showsHorizontalScrollIndicator=NO;
    _wTableVeiw.userInteractionEnabled= YES ;
    
    _wTableVeiw.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _wTableVeiw.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_wTableVeiw];
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    _wTableVeiw.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    // 马上进入刷新状态
    [_wTableVeiw.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    _wTableVeiw.mj_header.automaticallyChangeAlpha = YES;
    
    //默认block方法：设置上拉加载更多
    _wTableVeiw.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
}

-(void)loadData
{
    _pageNo = 1;
    [self requestDai];
}

-(void)loadNewData
{
    _pageNo++;
    [self requestDai];
}

-(void)endRefresh{
    [_wTableVeiw.mj_header endRefreshing];
    [_wTableVeiw.mj_footer endRefreshing];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.workArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MineWorkCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MineWorkCell" owner:nil options:nil] lastObject];
    _workModel= self.workArray[indexPath.row];
    cell.nameLab.text = _workModel.applyUserName;
    cell.typeLab.text = _workModel.businessName;
    cell.timeLab.text =_workModel.createTime;
    if(_workModel.status==1){
        cell.status.text =@"待审核";
        
    }else if (_workModel.status==2){
        cell.status.text =@"审核完成";
    }else if (_workModel.status==3){
        cell.status.text =@"打回";
    }
    return cell;
    
}

#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 69;
}


-(void)requestDai
{
    NSDictionary *params = @{@"currentUserId":[us objectForKey:kUserid],
                             @"page":@"1",
                             };
    [NetWorkManager requestType:1 withUrlString:self.url withParaments:params withSuccessBlock:^(id object) {
        [self endRefresh];
        if ([object[@"status"] isEqualToString:@"OK"] && [object[@"total"] intValue]>0 ) {
            if (_pageNo == 1) { // 说明是在重新请求数据.
                [self.workArray removeAllObjects];
            }
            NSArray * nowArray = [WorkModel mj_objectArrayWithKeyValuesArray:object[@"rows"]];
            [self.workArray addObjectsFromArray: nowArray];
            [_wTableVeiw reloadData];
            
            if (self.workArray.count < 10) {//本次接口获得的数据列表数<10
                _wTableVeiw.mj_footer.state = MJRefreshStateNoMoreData;;//显示无更多数据
            }
            
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
